import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
import 'package:sainbainu/common/models/response/version_list_response.dart';
import 'package:sainbainu/common/services/app_update.dart';
import 'package:sainbainu/common/values/colors.dart';

import 'index.dart';
import 'widgets/widgets.dart';

class VersionPage extends GetView<VersionController> {
  const VersionPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<VersionController>(
      builder: (_) {
        return Scaffold(
          appBar: AppBar(title: Text("版本号".tr)),
          body: EasyRefresh.builder(
            controller: _.state.refreshController,
            onLoad: _.onRichBottom,
            childBuilder: (context, physics) {
              return _buildBody(physics);
            },
          ),
        );
      },
    );
  }

  Widget _buildBody(ScrollPhysics physics) {
    return GetBuilder<VersionController>(
      id: 'list',
      builder: (_) {
        return ListView.separated(
          physics: physics,
          itemCount: _.state.versionListResponse.rows.length,
          padding: const EdgeInsets.all(0),
          separatorBuilder: (context, index) {
            return _buildDivider();
          },
          itemBuilder: (context, index) {
            var item = _.state.versionListResponse.rows[index];
            return _buildCellItem(
              title: 'v${item.versionName}',
              value: item.createTime ?? '',
              canUpdate: item.canUpdate,
              curVersion: item.curVersion,
              onTap: () => showVersionDetail(item),
            );
          },
        );
      },
    );
  }

  /// 分割线
  Widget _buildDivider() {
    return const Divider(
      height: 0,
      indent: 16,
      endIndent: 16,
      thickness: 0.5,
      color: AppColors.border,
    );
  }

  // 菜单每一项
  Widget _buildCellItem({
    required String title,
    required String value,
    bool canUpdate = false,
    bool curVersion = false,
    void Function()? onTap,
  }) {
    return ListTile(
      leading: Text(title),
      leadingAndTrailingTextStyle: const TextStyle(
        fontSize: 15,
        color: AppColors.active,
      ),
      title: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          if (canUpdate)
            Text('可更新'.tr, style: const TextStyle(color: AppColors.danger)),
          if (curVersion)
            Text('当前版本'.tr, style: const TextStyle(color: AppColors.primary)),
          const SizedBox(width: 8),
          Text(value, textAlign: TextAlign.right),
        ],
      ),
      titleTextStyle: const TextStyle(
        fontSize: 15,
        color: AppColors.unactive,
      ),
      tileColor: Colors.white,
      trailing: const Icon(Icons.keyboard_arrow_right),
      contentPadding: const EdgeInsets.only(left: 16, right: 10),
      enableFeedback: true,
      onTap: onTap,
    );
  }

  /// 版本详情页
  showVersionDetail(VersionItemResponse item) {
    showBarModalBottomSheet(
      context: Get.context!,
      builder: (context) {
        return VersionDetail(
          item: item,
          onUpdateTap: controller.onUpdateVersion,
        );
      },
    );
  }
}
